|
1
|
|
|
'use strict'; |
|
2
|
|
|
|
|
3
|
|
|
angular |
|
4
|
|
|
.module('game') |
|
5
|
|
|
.controller('main-loop', ['$scope', |
|
6
|
|
|
'$interval', |
|
7
|
|
|
'$timeout', |
|
8
|
|
|
'savegame', |
|
9
|
|
|
'state', |
|
10
|
|
|
function($scope, $interval, $timeout, savegame, state) { |
|
11
|
|
|
$scope.state = state; |
|
12
|
|
|
|
|
13
|
|
|
let self = this; |
|
14
|
|
|
let playerCopy = null; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Polyfill for Object.values from the TS39 Proposal. Reflect.ownKeys |
|
18
|
|
|
* swapped out for an equivalent using Object instead. Placing it here so |
|
19
|
|
|
* it'll fire before it's needed in ct_matter, and at the top of this |
|
20
|
|
|
* controller so it'll only fire once. |
|
21
|
|
|
* |
|
22
|
|
|
* Polyfill: https://github.com/tc39/proposal-object-values-entries |
|
23
|
|
|
* Reflect.ownKeys Alternative: |
|
24
|
|
|
* https://stackoverflow.com/questions/34449045/what-is-the-difference-between-reflect-ownkeysobj-and-object-keysobj |
|
25
|
|
|
*/ |
|
26
|
|
|
const reduce = Function.bind.call(Function.call, Array.prototype.reduce); |
|
27
|
|
|
const isEnumerable = Function.bind.call(Function.call, Object.prototype.propertyIsEnumerable); |
|
28
|
|
|
const concat = Function.bind.call(Function.call, Array.prototype.concat); |
|
29
|
|
|
const keys = (target) => Object.getOwnPropertyNames(target).concat(Object.getOwnPropertySymbols(target)); |
|
30
|
|
|
// const keys = Reflect.ownKeys; |
|
31
|
|
|
|
|
32
|
|
|
if (!Object.values) { |
|
33
|
|
|
Object.values = function values(O) { |
|
|
|
|
|
|
34
|
|
|
return reduce(keys(O), (v, k) => concat(v, typeof k === 'string' && isEnumerable(O, k) ? [O[k]] : []), []); |
|
35
|
|
|
}; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if (!Object.entries) { |
|
39
|
|
|
Object.entries = function entries(O) { |
|
|
|
|
|
|
40
|
|
|
return reduce(keys(O), (e, k) => concat(e, typeof k === 'string' && isEnumerable(O, k) ? [[k, O[k]]] : []), []); |
|
41
|
|
|
}; |
|
42
|
|
|
} |
|
43
|
|
|
/** End Polyfill */ |
|
44
|
|
|
|
|
45
|
|
|
self.update = function() { |
|
46
|
|
|
// do the update in a copy |
|
47
|
|
|
playerCopy = angular.copy(state.player); |
|
48
|
|
|
|
|
49
|
|
|
state.update(playerCopy); |
|
50
|
|
|
|
|
51
|
|
|
// and update all at once |
|
52
|
|
|
state.player = playerCopy; |
|
53
|
|
|
|
|
54
|
|
|
$timeout(self.update, 1000); |
|
55
|
|
|
}; |
|
56
|
|
|
|
|
57
|
|
|
self.startup = function() { |
|
58
|
|
|
savegame.load(); |
|
59
|
|
|
state.loading = false; |
|
60
|
|
|
$timeout(self.update, 1000); |
|
61
|
|
|
$interval(savegame.save, 10000); |
|
62
|
|
|
}; |
|
63
|
|
|
|
|
64
|
|
|
$timeout(self.startup); |
|
65
|
|
|
} |
|
66
|
|
|
]); |
|
67
|
|
|
|